﻿Imports System.Data.OleDb

Public Class BeachesList
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        myHeading.InnerHtml = "List of Beaches"

        If IsPostBack = False Then
            TagCloud.getTagCloud(SqlDataSource3, CloudTags, Page)
        End If
    End Sub

    Protected Sub btn_findByName_Click(sender As Object, e As EventArgs) Handles btn_findByName.Click
        searchDatabase("BeachName", tb_search.Text)
    End Sub

    Protected Sub btn_findByRegion_Click(sender As Object, e As EventArgs) Handles btn_findByRegion.Click
        searchDatabase("Region", tb_search.Text)
    End Sub

    Private Function searchDatabase(ByVal buttonStr As String, ByVal searchStr As String)
        SqlDataSource2.SelectCommand = "SELECT * FROM Beaches WHERE " & buttonStr & " LIKE '%" & searchStr & "%'"
        SqlDataSource2.DataBind()

        Return True
    End Function

    Protected Sub btn_Likes_Click(sender As Object, e As EventArgs) Handles btn_Likes.Click
        SqlDataSource2.SelectCommand = "SELECT * FROM BeachesWithMostLikes ORDER BY TotalLikes DESC"
        SqlDataSource2.DataBind()
        myHeading.InnerHtml = "Most Liked Beaches"
    End Sub

    Protected Sub btn_Rating_Click(sender As Object, e As EventArgs) Handles btn_Rating.Click
        SqlDataSource2.SelectCommand = "SELECT * FROM BeachesWithAverageRating ORDER BY AverageRating DESC"
        myHeading.InnerHtml = "Highest Rated Beaches"
    End Sub

    Protected Sub btn_Views_Click(sender As Object, e As EventArgs) Handles btn_Views.Click
        SqlDataSource2.SelectCommand = "SELECT * FROM BeachesWithMostViews ORDER BY TotalViews DESC"
        SqlDataSource2.DataBind()
        myHeading.InnerHtml = "Most Viewed Beaches"
    End Sub

    Protected Sub btn_Comments_Click(sender As Object, e As EventArgs) Handles btn_Comments.Click
        SqlDataSource2.SelectCommand = "SELECT * FROM BeachesWithMostComments ORDER BY TotalComments DESC"
        SqlDataSource2.DataBind()
        myHeading.InnerHtml = "Most discussed Beaches"
    End Sub

    Protected Sub SqlDataSource1_Selected(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource2.Selected
        If (e.AffectedRows < 1) Then
            myHeading.InnerHtml = "No Beach found"
        End If
        If (e.AffectedRows < 13) Then
            DataPagerBeaches.Visible = False
        Else
            DataPagerBeaches.Visible = True
        End If
    End Sub

    Protected Sub ListView1_DataBound(sender As Object, e As EventArgs)
        'Display datapager only if there is more than one page to display
        DataPagerBeaches.Visible = (DataPagerBeaches.PageSize < DataPagerBeaches.TotalRowCount)
    End Sub

   
    Protected Function getViews(ByVal beachID As Integer) As String
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "SELECT * FROM TotalViewsForAllBeaches WHERE ID = @f1"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", beachID)
        oleDbConn.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        dr.Read()
        Return dr("TotalViews")
        oleDbConn.Close()
        oleDbConn.Dispose()
    End Function
End Class